package com.pactera.miyuangroup.controller.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.pactera.miyuangroup.db.entity.EquipmentAudit;
import com.pactera.miyuangroup.db.entity.LoginUser;
import com.pactera.miyuangroup.db.entity.VendorInfo;
import com.pactera.miyuangroup.db.enums.AuditStatus;
import com.pactera.miyuangroup.filter.CurrentUser;
import com.pactera.miyuangroup.service.EquipmentAuditService;
import com.pactera.miyuangroup.service.LoginUserService;
import com.pactera.miyuangroup.service.VendorInfoService;
import com.pactera.miyuangroup.vo.convertor.VenderCodifyAuditConvert;
import com.pactera.miyuangroup.vo.request.web.PageVenderCodifyAuditReq;
import com.pactera.miyuangroup.vo.request.web.VenderCodifyAuditReq;
import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.User;
import com.pactera.miyuangroup.vo.response.web.PageVenderCodifyAuditRes;
import com.pactera.miyuangroup.vo.response.web.QueryVenderCodifyAuditRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 售货机信息修改审核
 *
 * @author Wangwu
 * @create 2018-04-23 上午10:45
 **/

@Api(description = "售货机信息修改审核")
@RestController
@RequestMapping("/venderAudit/")
public class VenderCodifyAuditController {

    @Autowired
    private EquipmentAuditService equipmentAuditService;
    @Autowired
    private VenderCodifyAuditConvert venderCodifyAuditConvert;
    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private VendorInfoService vendorInfoService;

    /***
     * 审核列表
     * @param page
     * @return
     */
    @ApiOperation(value = "售货机信息修改审核列表", notes = "售货机信息修改审核列表分页查询")
    @RequestMapping(value = "pageList", method = RequestMethod.POST)
    public ResultModel<Page<PageVenderCodifyAuditRes>> pageList(@CurrentUser User user, @RequestBody PageVenderCodifyAuditReq page) {
        return ResultModel.ok(equipmentAuditService.selectListPage(page, user.getOrgId()));
    }


    /***
     * 查询修改信息
     * @param equipmentId  审核表主键
     * @return
     */
    @ApiOperation(value = "查询修改信息", notes = "查询修改信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "审核ID", name = "equipmentId", required = true, paramType = "path")})
    @RequestMapping(value = "{equipmentId}", method = RequestMethod.GET)
    public ResultModel<QueryVenderCodifyAuditRes> getEquipmentAuditInfo(@PathVariable String equipmentId) {
        EquipmentAudit equipmentAudit = equipmentAuditService.selectById(equipmentId);
        LoginUser loginUser = loginUserService.selectById(equipmentAudit.getUserId());
        VendorInfo vendorInfo = vendorInfoService.selectById(equipmentAudit.getVendorId());
        QueryVenderCodifyAuditRes res = venderCodifyAuditConvert.convert2QueryVenderCodifyAudit(equipmentAudit, loginUser, vendorInfo);
        return ResultModel.ok(res);
    }


    /***
     * 审核
     * @param equipmentId  审核表主键
     * @return
     */
    @ApiOperation(value = "审核", notes = "审核")
    @ApiImplicitParams({@ApiImplicitParam(value = "审核ID", name = "equipmentId", required = true, paramType = "path")})
    @RequestMapping(value = "/audit/{equipmentId}", method = RequestMethod.POST)
    public ResultModel auditInfo(@PathVariable String equipmentId, @RequestBody VenderCodifyAuditReq req) {
        EquipmentAudit equipmentAudit = equipmentAuditService.selectById(equipmentId);
        if (null == equipmentAudit) {
            ResultModel.fail();
        }
        equipmentAudit.setAuditStatus(req.getStatus());
        boolean flg = equipmentAuditService.updateById(equipmentAudit);
        if (equipmentAudit.getAuditStatus().equals(AuditStatus.ADOPT)) {
            VendorInfo vendorInfo = vendorInfoService.selectById(equipmentAudit.getVendorId());
            vendorInfo.setNumber(equipmentAudit.getAfterVendorNumber());
            flg = vendorInfoService.updateById(vendorInfo);
        }
        if (!flg) {

            ResultModel.fail();
        }
        return ResultModel.ok();
    }


}
